package com.bcu.cn.onlineexamsystem.exammanage.mapper;

import com.bcu.cn.onlineexamsystem.exammanage.entity.ExamReviewDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ExamReviewMapper {
    
    // 查询教师待批改的考试记录
    @Select("SELECT se.record_id, se.student_id, u.real_name as student_name, " +
            "se.exam_id, e.exam_name, c.course_name, se.submit_time " +
            "FROM student_exams se " +
            "JOIN exams e ON se.exam_id = e.exam_id " +
            "JOIN courses c ON e.course_id = c.course_id " +
            "JOIN users u ON se.student_id = u.user_id " +
            "WHERE e.teacher_id = #{teacherId} AND se.status = 2 AND se.total_score IS NULL " +
            "ORDER BY se.submit_time DESC")
    List<ExamReviewDTO> findPendingExamsByTeacher(@Param("teacherId") String teacherId);
    
    // 查询单个考试记录的详细答题信息
    @Select("SELECT sa.answer_id, sa.question_id, q.content as question_content, " +
            "q.type as question_type, sa.answer as student_answer, q.answer as correct_answer, " +
            "q.score as max_score, sa.score, sa.teacher_comment " +
            "FROM student_answers sa " +
            "JOIN questions q ON sa.question_id = q.question_id " +
            "WHERE sa.record_id = #{recordId} " +
            "ORDER BY sa.question_id")
    List<ExamReviewDTO.AnswerReviewDTO> findAnswersByRecordId(@Param("recordId") Long recordId);
    
    // 更新学生答案的分数和评语
    @Update("UPDATE student_answers SET score = #{score}, teacher_comment = #{teacherComment} " +
            "WHERE answer_id = #{answerId}")
    int updateAnswerScore(@Param("answerId") Long answerId, 
                         @Param("score") java.math.BigDecimal score, 
                         @Param("teacherComment") String teacherComment);
    
    // 更新考试记录的总分
    @Update("UPDATE student_exams SET total_score = #{totalScore} WHERE record_id = #{recordId}")
    int updateExamTotalScore(@Param("recordId") Long recordId, 
                            @Param("totalScore") java.math.BigDecimal totalScore);
    
    // 根据recordId查询考试记录
    @Select("SELECT se.record_id, se.student_id, u.real_name as student_name, " +
            "se.exam_id, e.exam_name, c.course_name, se.submit_time " +
            "FROM student_exams se " +
            "JOIN exams e ON se.exam_id = e.exam_id " +
            "JOIN courses c ON e.course_id = c.course_id " +
            "JOIN users u ON se.student_id = u.user_id " +
            "WHERE se.record_id = #{recordId}")
    ExamReviewDTO findExamByRecordId(@Param("recordId") Long recordId);
    
    // 根据recordId和teacherId查询考试记录（用于权限验证）
    @Select("SELECT se.record_id, se.student_id, u.real_name as student_name, " +
            "se.exam_id, e.exam_name, c.course_name, se.submit_time " +
            "FROM student_exams se " +
            "JOIN exams e ON se.exam_id = e.exam_id " +
            "JOIN courses c ON e.course_id = c.course_id " +
            "JOIN users u ON se.student_id = u.user_id " +
            "WHERE se.record_id = #{recordId} AND e.teacher_id = #{teacherId}")
    ExamReviewDTO findExamByRecordIdAndTeacher(@Param("recordId") Long recordId, @Param("teacherId") String teacherId);
} 